<?php

namespace App\Http\Controllers\School;

use App\Http\Requests\School\EditPriceCategory;
use App\Http\Requests\School\StorePriceCategory;
use App\Http\Resources\School\PriceCategoryResource;
use App\Models\PriceCategory;
use App\Models\SchoolAdminLog;
use Illuminate\Http\Request;

/**
 *
 * 条目管理
 *
 * Class PriceCategoryController
 * @package App\Http\Controllers\School
 */
class PriceCategoryController extends BaseController
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     * 条目列表
     */
    public function index(Request $request)
    {
        $priceCategory = PriceCategory::query()->latest()
            ->where('school_id', \Auth::user()->school_id)
            ->nameLike($request->name)
            ->paginate($request->size);
        return PriceCategoryResource::collection($priceCategory);
    }

    /**
     * @param StorePriceCategory $request
     * @return PriceCategoryResource
     * 新增条目
     */
    public function store(StorePriceCategory $request)
    {
        $priceCategory = PriceCategory::create(array_merge($request->all(), [
            'school_id' => \Auth::user()->school_id
        ]));
        SchoolAdminLog::Log('新增条目: ' . $priceCategory->name);
        return new PriceCategoryResource($priceCategory);
    }

    /**
     * @param EditPriceCategory $request
     * @param PriceCategory $priceCategory
     * @return PriceCategoryResource
     * 编辑条目
     */
    public function update(EditPriceCategory $request, PriceCategory $priceCategory)
    {
        $priceCategory->fill($request->all());
        $priceCategory->save();
        SchoolAdminLog::Log('编辑条目: ' . $priceCategory->name);
        return new PriceCategoryResource($priceCategory);
    }
}
